package com.disdr.picture;

import cn.hutool.core.lang.PatternPool;
import cn.hutool.core.lang.Validator;
import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.springframework.util.StringUtils;

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 读取图片中的文字
 *
 */
public class Picture {

    /**
     * 双禄回单识别
     * @param args
     */
    public static void main(String[] args) throws TesseractException {
        //加载待读取图片
        File imageFile = new File("E://yyyy.png");
        //创建tess对象
        ITesseract instance = new Tesseract();
        //设置训练文件目录
        instance.setDatapath("E:\\tessdata");
        //设置训练语言
        instance.setLanguage("chi_sim");
        //执行转换
        String result = instance.doOCR(imageFile);
        System.out.println(result);


        fine(result,"[0-9]{11}");
        fine(result,"[0-9]{17}[0-9]|X|x");//18位身份证

    }

    //正则匹配
    private static String fine(String source,String pattern){
        //String pattern = "[0-9]{18}";//
        Pattern p1 = PatternPool.PLATE_NUMBER;
        Pattern p = Pattern.compile(pattern);
        Matcher matcher = p.matcher(source);
        if (matcher.find()) {
            String str = matcher.group();
            System.out.println(str);
            return str;
        }
       return "";
    }
}
